package com.treetao.union.mall.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.treetao.union.db.model.CommonUser;
import com.treetao.union.db.model.MallOrderDetail;
import com.treetao.union.db.model.MallPComment;
import com.treetao.union.db.model.MallPSku;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.db.service.MallOrderDetailService;
import com.treetao.union.db.service.MallPCommentService;
import com.treetao.union.db.service.MallPSkuService;
import com.treetao.union.mall.api.base.BaseCURDController;
import com.treetao.union.mall.api.vo.req.ReqParam;
import com.treetao.union.mall.api.vo.resp.PageResp;
@RestController
@RequestMapping("/mall/comment")
public class MallCommentController extends BaseCURDController {
	@Autowired
	private MallPCommentService mallPCommentService;
	@Autowired
	private MallPSkuService mallPSkuService;
	@Autowired
	private MallOrderDetailService mallOrderDetailService;

	@Override
	protected IService<?> getCommonService() {
		return mallPCommentService;
	}
	

	/**
	 * 订单评论
	 * @param param
	 * @return
	 */
	@PostMapping("add")
	public RestResponse comment(@RequestBody ReqParam param) {
		Long orderno = param.getLong("orderno", false);
		Long skuId = param.getLong("skuId", false);
		Integer star = param.getInt("star", false);
		String content = param.getValue("content", false);
		Long userId = getWxuidFromToken();
		
		MallOrderDetail detail = mallOrderDetailService.getDetail(orderno,skuId);
		if(detail==null) {
			return RestResponse.failure("无效订单");
		}
		if(detail.getCommented()) {
			return RestResponse.failure("请不要重复评论");
		}
		
		boolean flag = mallPCommentService.saveComment(detail, userId, star, content);
		
		return RestResponse.success(flag);
	}

	@Override
	protected RestResponse page() {
		ReqParam rp = getMappingReqParam();
		Long skuId = rp.getLong("spuId");
		String skuNo = rp.getValue("skuNo");
		
		QueryWrapper<?> q = getQueryWrapperNoDel();
		if(skuId!=null) {
			q.eq("sku_id", skuId);
		} else if(skuNo!=null){
			MallPSku sku = mallPSkuService.getSku(skuNo);
			if(sku!=null) {
				q.eq("sku_id", sku.getId());
			}
		}
		q.orderByDesc("star","comment_time");
		
		IPage<?> ipage = mallPCommentService.custPageDataRef(new Page<>(rp.getPageCurrent(), rp.getPageSize()), q);
		
		PageResp resp = conver2pageData(ipage);
		if(resp.getItems()!=null) {
			//过滤掉不需要返回的字段
			for(Object obj : resp.getItems()) {
				MallPComment comm = (MallPComment)obj;
				if(comm.getUserId()!=null) {
					CommonUser user = comm.getUser();
					if(user!=null) {
						user.setDelFlag(null);
						user.setPhone(null);
						user.setRegtime(null);
						user.setSessionkey(null);
						user.setSex(null);
						user.setWxopenid(null);
						user.setWxunionid(null);
					}
					comm.setUser(user);
				}
			}
		}
		return RestResponse.success(resp);
	}
}
